package php.code.indentation;

import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;

public class Indentable {
    public final Property<Indentation> indentationProperty = new SimpleObjectProperty<>(Indentation.NONE);

    private final String tab;

    public Indentable(Indentation indentation, String tab) {
        this.tab = tab;
        setIndentation(indentation);
    }

    public Indentable(Indentable parent, String tab){
        this.tab = tab;
        setParent(parent);
    }

    public Indentable(Indentable parent) {
        this(parent, Indentation.DEFAULT);
    }

    public Indentable() {
        this(Indentation.NONE);
    }

    public Indentable(Indentation indentation) {
        this(indentation, Indentation.DEFAULT);
    }

    public Indentation getIndentation(){
        return indentationProperty.getValue();
    }

    public void setParent(Indentable parent) {
        parent.indentationProperty.addListener((observable, oldValue, newValue) -> setIndentation(parent.getChildIndentation()));
        setIndentation(parent.getChildIndentation());
    }

    protected Indentation getChildIndentation() {
        return new Indentation(getIndentation(), tab);
    }

    public void setIndentation(Indentation indentation) {
        indentationProperty.setValue(indentation);
    }
}
